home *** CD-ROM | disk | FTP | other *** search
- package javax.swing.text;
-
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Toolkit;
- import java.io.IOException;
- import java.io.NotSerializableException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
- import javax.swing.SwingUtilities;
- import javax.swing.event.ChangeListener;
-
- public class StyleContext implements Serializable, AbstractDocument.AttributeContext {
- private static StyleContext defaultContext;
- public static final String DEFAULT_STYLE = "default";
- private static Hashtable freezeKeyMap;
- private static Hashtable thawKeyMap;
- private Style styles = new NamedStyle(this, (Style)null);
- private transient FontKey fontSearch = new FontKey((String)null, 0, 0);
- private transient Hashtable fontTable = new Hashtable();
- private transient Hashtable attributesPool = new Hashtable();
- private transient MutableAttributeSet search = new SimpleAttributeSet();
- private int unusedSets;
- static final int THRESHOLD = 9;
- // $FF: synthetic field
- static Class class$javax$swing$event$ChangeListener;
-
- public StyleContext() {
- this.addStyle("default", (Style)null);
- }
-
- public synchronized AttributeSet addAttribute(AttributeSet var1, Object var2, Object var3) {
- if (var1.getAttributeCount() + 1 <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(var1);
- this.search.addAttribute(var2, var3);
- this.reclaim(var1);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet var4 = this.getMutableAttributeSet(var1);
- var4.addAttribute(var2, var3);
- return var4;
- }
- }
-
- public synchronized AttributeSet addAttributes(AttributeSet var1, AttributeSet var2) {
- if (var1.getAttributeCount() + var2.getAttributeCount() <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(var1);
- this.search.addAttributes(var2);
- this.reclaim(var1);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
- var3.addAttributes(var2);
- return var3;
- }
- }
-
- public void addChangeListener(ChangeListener var1) {
- this.styles.addChangeListener(var1);
- }
-
- public Style addStyle(String var1, Style var2) {
- NamedStyle var3 = new NamedStyle(this, var1, var2);
- if (var1 != null) {
- this.styles.addAttribute(var1, var3);
- }
-
- return var3;
- }
-
- // $FF: synthetic method
- static Class class$(String var0) {
- try {
- return Class.forName(var0);
- } catch (ClassNotFoundException var2) {
- throw new NoClassDefFoundError(((Throwable)var2).getMessage());
- }
- }
-
- protected MutableAttributeSet createLargeAttributeSet(AttributeSet var1) {
- return new SimpleAttributeSet(var1);
- }
-
- protected SmallAttributeSet createSmallAttributeSet(AttributeSet var1) {
- return new SmallAttributeSet(this, var1);
- }
-
- public Color getBackground(AttributeSet var1) {
- return StyleConstants.getForeground(var1);
- }
-
- protected int getCompressionThreshold() {
- return 9;
- }
-
- public static final StyleContext getDefaultStyleContext() {
- if (defaultContext == null) {
- defaultContext = new StyleContext();
- }
-
- return defaultContext;
- }
-
- public AttributeSet getEmptySet() {
- return SimpleAttributeSet.EMPTY;
- }
-
- public Font getFont(String var1, int var2, int var3) {
- this.fontSearch.setValue(var1, var2, var3);
- Font var4 = (Font)this.fontTable.get(this.fontSearch);
- if (var4 == null) {
- var4 = new Font(var1, var2, var3);
- FontKey var5 = new FontKey(var1, var2, var3);
- this.fontTable.put(var5, var4);
- }
-
- return var4;
- }
-
- public Font getFont(AttributeSet var1) {
- int var2 = 0;
- if (StyleConstants.isBold(var1)) {
- var2 |= 1;
- }
-
- if (StyleConstants.isItalic(var1)) {
- var2 |= 2;
- }
-
- String var3 = StyleConstants.getFontFamily(var1);
- int var4 = StyleConstants.getFontSize(var1);
- if (StyleConstants.isSuperscript(var1) || StyleConstants.isSubscript(var1)) {
- var4 -= 2;
- }
-
- return this.getFont(var3, var2, var4);
- }
-
- public FontMetrics getFontMetrics(Font var1) {
- return Toolkit.getDefaultToolkit().getFontMetrics(var1);
- }
-
- public Color getForeground(AttributeSet var1) {
- return StyleConstants.getForeground(var1);
- }
-
- AttributeSet getImmutableUniqueSet() {
- SmallAttributeSet var1 = this.createSmallAttributeSet(this.search);
- SmallAttributeSet var2 = (SmallAttributeSet)this.attributesPool.get(var1);
- if (var2 == null) {
- var2 = var1;
- this.attributesPool.put(var1, var1);
- }
-
- ++var2.nrefs;
- return var2;
- }
-
- MutableAttributeSet getMutableAttributeSet(AttributeSet var1) {
- return var1 instanceof MutableAttributeSet && var1 != SimpleAttributeSet.EMPTY ? (MutableAttributeSet)var1 : this.createLargeAttributeSet(var1);
- }
-
- public static Object getStaticAttribute(Object var0) {
- return thawKeyMap != null && var0 != null ? thawKeyMap.get(var0) : null;
- }
-
- public static Object getStaticAttributeKey(Object var0) {
- return var0.getClass().getName() + "." + var0.toString();
- }
-
- public Style getStyle(String var1) {
- return (Style)this.styles.getAttribute(var1);
- }
-
- public Enumeration getStyleNames() {
- return this.styles.getAttributeNames();
- }
-
- public static void readAttributeSet(ObjectInputStream var0, MutableAttributeSet var1) throws ClassNotFoundException, IOException {
- int var2 = var0.readInt();
-
- for(int var3 = 0; var3 < var2; ++var3) {
- Object var4 = var0.readObject();
- Object var5 = var0.readObject();
- if (thawKeyMap != null) {
- Object var6 = thawKeyMap.get(var4);
- if (var6 != null) {
- var4 = var6;
- }
-
- Object var7 = thawKeyMap.get(var5);
- if (var7 != null) {
- var5 = var7;
- }
- }
-
- var1.addAttribute(var4, var5);
- }
-
- }
-
- public void readAttributes(ObjectInputStream var1, MutableAttributeSet var2) throws ClassNotFoundException, IOException {
- readAttributeSet(var1, var2);
- }
-
- private void readObject(ObjectInputStream var1) throws ClassNotFoundException, IOException {
- this.fontSearch = new FontKey((String)null, 0, 0);
- this.fontTable = new Hashtable();
- this.search = new SimpleAttributeSet();
- this.attributesPool = new Hashtable();
- var1.defaultReadObject();
- }
-
- public void reclaim(AttributeSet var1) {
- if (var1 instanceof SmallAttributeSet) {
- SmallAttributeSet var2 = (SmallAttributeSet)var1;
- --var2.nrefs;
- if (var2.nrefs <= 0) {
- ++this.unusedSets;
- if (this.unusedSets > 10 && this.unusedSets > this.attributesPool.size() / 10) {
- if (SwingUtilities.isEventDispatchThread()) {
- this.removeUnusedSets();
- } else {
- 1 var3 = new 1(this);
- SwingUtilities.invokeLater(var3);
- }
- }
- }
- }
-
- }
-
- public static void registerStaticAttributeKey(Object var0) {
- String var1 = var0.getClass().getName() + "." + var0.toString();
- if (freezeKeyMap == null) {
- freezeKeyMap = new Hashtable();
- thawKeyMap = new Hashtable();
- }
-
- freezeKeyMap.put(var0, var1);
- thawKeyMap.put(var1, var0);
- }
-
- public synchronized AttributeSet removeAttribute(AttributeSet var1, Object var2) {
- if (var1.getAttributeCount() - 1 <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(var1);
- this.search.removeAttribute(var2);
- this.reclaim(var1);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
- var3.removeAttribute(var2);
- return var3;
- }
- }
-
- public synchronized AttributeSet removeAttributes(AttributeSet var1, Enumeration var2) {
- if (var1.getAttributeCount() <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(var1);
- this.search.removeAttributes(var2);
- this.reclaim(var1);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
- var3.removeAttributes(var2);
- return var3;
- }
- }
-
- public synchronized AttributeSet removeAttributes(AttributeSet var1, AttributeSet var2) {
- if (var1.getAttributeCount() <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(var1);
- this.search.removeAttributes(var2);
- this.reclaim(var1);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet var3 = this.getMutableAttributeSet(var1);
- var3.removeAttributes(var2);
- return var3;
- }
- }
-
- public void removeChangeListener(ChangeListener var1) {
- this.styles.removeChangeListener(var1);
- }
-
- public void removeStyle(String var1) {
- this.styles.removeAttribute(var1);
- }
-
- synchronized void removeUnusedSets() {
- Vector var1 = new Vector();
- Enumeration var2 = this.attributesPool.keys();
-
- while(var2.hasMoreElements()) {
- SmallAttributeSet var3 = (SmallAttributeSet)var2.nextElement();
- if (var3.nrefs <= 0) {
- var1.addElement(var3);
- }
- }
-
- var2 = var1.elements();
-
- while(var2.hasMoreElements()) {
- this.attributesPool.remove(var2.nextElement());
- }
-
- this.unusedSets = 0;
- }
-
- public String toString() {
- this.removeUnusedSets();
- String var1 = "";
-
- SmallAttributeSet var3;
- for(Enumeration var2 = this.attributesPool.keys(); var2.hasMoreElements(); var1 = var1 + var3 + "\n") {
- var3 = (SmallAttributeSet)var2.nextElement();
- }
-
- return var1;
- }
-
- public static void writeAttributeSet(ObjectOutputStream var0, AttributeSet var1) throws IOException {
- int var2 = var1.getAttributeCount();
- var0.writeInt(var2);
- Enumeration var3 = var1.getAttributeNames();
-
- while(var3.hasMoreElements()) {
- Object var4 = var3.nextElement();
- if (var4 instanceof Serializable) {
- var0.writeObject(var4);
- } else {
- Object var5 = freezeKeyMap.get(var4);
- if (var5 == null) {
- throw new NotSerializableException(var4.getClass().getName() + " is not serializable as a key in an AttributeSet");
- }
-
- var0.writeObject(var5);
- }
-
- Object var7 = var1.getAttribute(var4);
- if (var7 instanceof Serializable) {
- var0.writeObject(var7);
- } else {
- Object var6 = freezeKeyMap.get(var7);
- if (var6 == null) {
- throw new NotSerializableException(var7.getClass().getName() + " is not serializable as a value in an AttributeSet");
- }
-
- var0.writeObject(var6);
- }
- }
-
- }
-
- public void writeAttributes(ObjectOutputStream var1, AttributeSet var2) throws IOException {
- writeAttributeSet(var1, var2);
- }
-
- private void writeObject(ObjectOutputStream var1) throws IOException {
- this.removeUnusedSets();
- var1.defaultWriteObject();
- }
- }
-